package com.rockcent.mall.lib.helper;

import com.alibaba.fastjson.JSON;
import com.rockcent.common.common.BusinessException;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 阿里大于短信网关
 * Created by zhangjh on 2017/5/4.
 */
@Service
public class AlidayuSMSStrategy implements SMSStrategy {
    private static Logger logger = LoggerFactory.getLogger(AlidayuSMSStrategy.class);

    private final static String URL = "http://gw.api.taobao.com/router/rest";

    private final static String APPKEY = "23426795";
    private final static String SECRET = "2b6de1746f011055dcae8548175c5235";
    private final static String Sms_Free_SignName = "络享";
    private final static String SmsType = "normal";
    private final static String SMS_TEMPLATE_CODE_HAVE_NAME = "SMS_66465273";//有子平台名称


    @Override
    public String send(String mobile, String code, String name) throws BusinessException {
        String response;
        try {
            TaobaoClient client = new DefaultTaobaoClient(URL, APPKEY, SECRET);
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            req.setExtend(mobile);//公共回传参数
            req.setSmsType(SmsType);//短信类型，传入值请填写normal
            req.setSmsFreeSignName(Sms_Free_SignName);//短信签名，传入的短信签名必须是在阿里大于“管理中心-验证码/短信通知/推广短信-配置短信签名”中的可用签名。
            req.setRecNum(mobile);//短信接收号码。支持单个或多个手机号码，传入号码为11位手机号码，不能加0或+86。群发短信需传入多个号码，以英文逗号分隔，一次调用最多传入200个号码。示例：18600000000,13911111111,13322222222
            setSmsParamString(code, name, req);
//        req.setSmsParamString("{\"code\":\"" + code + "\",\"product\":\"" + name + "\"}");//短信模板变量，传参规则{"key":"value"}，key的名字须和申请模板中的变量名一致，多个变量之间以逗号隔开。示例：针对模板“验证码${code}，您正在进行${product}身份验证，打死不要告诉别人哦！”，传参时需传入{"code":"1234","product":"alidayu"}
            req.setSmsTemplateCode(SMS_TEMPLATE_CODE_HAVE_NAME);//短信模板ID，传入的模板必须是在阿里大于“管理中心-短信模板管理”中的可用模板。
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
//        System.out.println(rsp.getBody());
            response = rsp.getBody();
        } catch (ApiException e) {
            logger.info(e.getMessage(), e);
            throw new BusinessException("发送验证码失败");
        }


        return response;
    }

    private void setSmsParamString(String code, String name, AlibabaAliqinFcSmsNumSendRequest req) {

        if (StringUtils.isBlank(name)) {
            name = Sms_Free_SignName;
        }
        req.setSmsParamString("{\"code\":\"" + code + "\",\"product\":\"" + name + "\"}");//短信模板变量，传参规则{"key":"value"}，key的名字须和申请模板中的变量名一致，多个变量之间以逗号隔开。示例：针对模板“验证码${code}，您正在进行${product}身份验证，打死不要告诉别人哦！”，传参时需传入{"code":"1234","product":"alidayu"}
    }

    @Override
    public String send(String mobile, String code) throws BusinessException {
        return send(mobile, code, null);
    }

    @Override
    public boolean handleResponse(String resultBody) {
        AlibabaSmsSendResponse respon = JSON.parseObject(resultBody, AlibabaSmsSendResponse.class);
        boolean result = false;
        if (null != respon) {
            result = respon.getAlibaba_aliqin_fc_sms_num_send_response().getResult().getSuccess();
        }
        return result;
    }

    class AlibabaSmsSendResponse {

        private AlibabaAliqinSmsSendResponse alibaba_aliqin_fc_sms_num_send_response;

        public AlibabaAliqinSmsSendResponse getAlibaba_aliqin_fc_sms_num_send_response() {
            return alibaba_aliqin_fc_sms_num_send_response;
        }

        public void setAlibaba_aliqin_fc_sms_num_send_response(AlibabaAliqinSmsSendResponse alibaba_aliqin_fc_sms_num_send_response) {
            this.alibaba_aliqin_fc_sms_num_send_response = alibaba_aliqin_fc_sms_num_send_response;
        }
    }

    class AlibabaAliqinSmsSendResponse {
        private String request_id;

        private AlibabaAliqinSmsSendResponseResult result;

        public String getRequest_id() {
            return request_id;
        }

        public void setRequest_id(String request_id) {
            this.request_id = request_id;
        }

        public AlibabaAliqinSmsSendResponseResult getResult() {
            return result;
        }

        public void setResult(AlibabaAliqinSmsSendResponseResult result) {
            this.result = result;
        }
    }

    class AlibabaAliqinSmsSendResponseResult {
        private String err_code;
        private String model;
        private boolean success;

        public String getErr_code() {
            return err_code;
        }

        public String getModel() {
            return model;
        }

        public boolean getSuccess() {
            return success;
        }

        public void setErr_code(String err_code) {
            this.err_code = err_code;
        }

        public void setModel(String model) {
            this.model = model;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }
    }

    //
    public static void main(String[] args) throws ApiException {
//        String content = "悦容美：尊敬的用户您正在登陆验证，验证码927121，请在1分钟内按页面提示提交验证码，请勿将验证码泄露于他人。";
        String code = "208811";
        String product = "悦容美";

        new AlidayuSMSStrategy().send("18664915818", code, product);
    }


}
